﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 音频管理器
/// </summary>
public class AudioManager : MonoBehaviour
{
    public static AudioManager Instance;

    [Header("按钮点击")]
    public AudioClip buttonClip;
    [Header("方块清除")]
    public AudioClip cancleClip;
    [Header("重新洗牌")]
    public AudioClip reshuffleClip;
    [Header("胜利")]
    public AudioClip winClip;
    [Header("失败")]
    public AudioClip failClip;

    // 音效开关
    private bool sound;
    public bool Sound
    {
        get => sound;
        set
        {
            sound = value;
            if (value)
            {
                PlayerPrefs.SetInt("Sound", 1);
                AudioListener.volume = 1;
            }
            else
            {
                PlayerPrefs.SetInt("Sound", 0);
                AudioListener.volume = 0;
            }
        }
    }

    private AudioSource audioSource;

    private void Awake()
    {
        if (Instance)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
            DontDestroyOnLoad(this);
            audioSource = GetComponent<AudioSource>();
            if (PlayerPrefs.GetInt("Sound", 1) == 1)
                Sound = true;
            else
                Sound = false;
        }
    }


    /// <summary>
    /// 播放按钮点击音效
    /// </summary>
    public void PlayButtonSound()
    {
        if (buttonClip)
            audioSource.PlayOneShot(buttonClip);
    }

    /// <summary>
    /// 播放方块清除音效
    /// </summary>
    public void PlayCancleSound()
    {
        if (cancleClip)
            audioSource.PlayOneShot(cancleClip);
    }

    /// <summary>
    /// 播放重新洗牌音效
    /// </summary>
    public void PlayReshuffleSound()
    {
        if (reshuffleClip)
            audioSource.PlayOneShot(reshuffleClip, 1f);
    }

    /// <summary>
    /// 播放游戏胜利音效
    /// </summary>
    public void PlayWinSound()
    {
        if (winClip)
            audioSource.PlayOneShot(winClip);
    }

    /// <summary>
    /// 播放游戏失败音效
    /// </summary>
    public void PlayFailSound()
    {
        if (failClip)
            audioSource.PlayOneShot(failClip);
    }


}
